package com.changpei.core.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.changpei.common.cache.ContentCache;
import com.changpei.common.utils.FileUtil;
import com.changpei.core.config.AliYunOssUtil;
import com.changpei.core.pojo.*;
import com.changpei.core.utils.JsoupUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 *
 * @author Way on 2021/9/20
 */
@SuppressWarnings("all")
public class ExtractData extends ApplicationTest {

    public static final String FILE_PATH = "/Users/weibiao/Downloads/临时文件/uploaded_files/";

    @Autowired
    private UserService userService;

    @Autowired
    private AliYunOssUtil aliYunOssUtil;

    @Autowired
    private ContentService contentService;

    @Autowired
    private TagsService tagsService;

    @Autowired
    private FileService fileService;

    @Autowired
    private ContentFileService contentFileService;

    @Autowired
    private ContentTagsService contentTagsService;

    @Autowired
    private ContentCache contentCache;

    @Autowired
    private ContentTmpService contentTmpService;

    @Test
    public void test() {
        System.out.println(1 % 8);
    }

    @Test
    public void exec() throws IOException {
        /**
         * 1,推荐
         * 2,情侣
         * 3,男生
         * 4,女生
         * 5,卡通动漫
         * 6,风景静物
         * 7,唯美
         * 8,可爱
         * 9,文字
         * 10,欧美 --
         * 11,影视明星
         * 12,体育
         * 13,汽车品牌
         * 14,其他
         */
        // 附带壁纸
        boolean bz = true;
        for (int i = 0; i < 40; i++) {
            System.out.println("抓取内容页码>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + i);
            if (i < 1) {
                entrence("https://www.woyaogexing.com/shouji/weimei/", 14L, bz);
            } else {
                entrence("https://www.woyaogexing.com/shouji/weimei/index_" + (i + 1) + ".html", 14L, bz);
            }
        }
    }

    /**
     * 跑批入口
     * @param url
     * @param isCelebrityUp
     */
    public void entrence(String url, Long categoryId, Boolean bz) throws IOException {
        List<String> contents = getContent(url);
        for (String content : contents) {
            Map<String, Object> userInfo = getUserInfo(content);
            if (!userInfo.isEmpty()) {
                Long userId = Long.parseLong(userInfo.get("userId").toString());
                Document document = (Document) userInfo.get("document");
                getContentDetail(document, userId, categoryId, bz);
            }
        }
    }

    /**
     * 获取内容详情
     * @param userId
     */
    public void getContentDetail(Document document, Long userId, Long categoryId, Boolean bz) throws IOException {

        StringBuffer sb = new StringBuffer();
        String content = document.getElementsByClass("pifutitle").text();
        if (StringUtils.isNotEmpty(content)) {
            sb.append(content);
        } else {
            String another = document.getElementsByClass("contLeftA")
                    .get(0).getElementsByTag("h1").text();
            sb.append(another);
        }

        System.out.println("抓取内容详情>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + sb);
//        Content sysContent = new Content();
//        sysContent.setContent(sb.toString());
//        sysContent.setUserId(userId);
//        sysContent.setCategoryId(categoryId);
//        contentService.save(sysContent);
//        Long contentId = sysContent.getId();

        Long contentId = contentCache.getSeqContentTmpId();
        ContentTmp contentTmp = new ContentTmp();
        contentTmp.setContent(sb.toString());
        contentTmp.setUserId(userId);
        contentTmp.setCategoryId(categoryId);
        contentTmp.setId(contentId);
        contentTmpService.save(contentTmp);

        // 获取内容图片
        List<Element> elements = new ArrayList<>();
        // 头像
        Elements e1 = document.getElementsByClass("tx-img");
        // 美图
        Elements e2 = document.getElementsByClass("tx-img new_p");
        // 壁纸
        Element e3 = document.getElementsByClass("artCont cl").get(0);
        if (!e1.isEmpty()) {
            elements.addAll(e1);
            System.out.println("头像图片个数>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + e1.size());
        }
        if (!e2.isEmpty()) {
            elements.addAll(e2);
            System.out.println("美图图片个数>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + e2.size());
        }
        if (!ObjectUtils.isEmpty(e3) && bz) {
            elements.add(e3);
            System.out.println("壁纸图片个数>>>>>>>>>>>>>>>>>>>>>>>>>>>> + 1");
        }

        List<String> imgsUrls = elements.stream().map(element -> {
            Elements a = element.getElementsByTag("a");
            System.out.println("内容图片地址>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + a.attr("href"));
            String href = getAvailableUrl(a.attr("href"));
            return href;
        }).collect(Collectors.toList());

        System.out.println("内容图片个数>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + imgsUrls.size());

        for (String imgsUrl : imgsUrls) {

            // 保存网络图片至本地
            String contentFileName = FileUtil.downloadPicture(imgsUrl, FILE_PATH);

            // 内容图片信息入库
            com.changpei.core.pojo.File sysFile = new com.changpei.core.pojo.File();

            java.io.File img = new java.io.File(FILE_PATH + contentFileName);
            BufferedImage sourceImg = ImageIO.read(new FileInputStream(img));

            if (!ObjectUtils.isEmpty(sourceImg)) {
                sysFile.setWidth(sourceImg.getWidth());
                sysFile.setHeight(sourceImg.getHeight());
            }

            sysFile.setUrl(contentFileName);
            fileService.save(sysFile);
            Long fileId = sysFile.getId();

            // 建立内容与文件关联关系
            ContentFile sysContentFile = new ContentFile();
            sysContentFile.setContentId(contentId);
            sysContentFile.setFileId(fileId);
            contentFileService.save(sysContentFile);

            // 上传oss -> 内容目录
            String contentReq = aliYunOssUtil.upload(contentFileName, "content");
            System.out.println("上传至序列号>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + contentReq);

            // 获取通用打标
//            String tagFileUrl = "https://changpei-oss.oss-cn-shanghai.aliyuncs.com/content/" + contentFileName;
//            JSONArray data = filesFeign.taggingImage(tagFileUrl).getData();


//            if (!data.isEmpty()) {
//                JsonUtil.prettyPrintByJson(data);
//
//                List<Tags> tags = JsonUtil.jsonToList(data, Tags.class);
//
//                if (isCelebrityUp) {
//                    JSONArray famousMen = filesFeign.detectCelebrity(tagFileUrl).getData();
//
//                    for (Object man : famousMen) {
//                        JSONObject object = JSONObject.parseObject(JSON.toJSONString(man));
//                        Tags tags1 = new Tags();
//                        tags1.setValue(object.getString("name"));
//                        tags1.setConfidence(99.0);
//                        tags.add(tags1);
//                    }
//                }
//
//                JsonUtil.prettyPrintByJson(tags);
//
//                for (Tags tag : tags) {
//                    // 获取新增ID
//                    Long tagId = tagsFeign.add(tag).getData();
//
//                    // 保存关联关系
//                    FileTags fileTags = new FileTags();
//                    fileTags.setFileId(fileId);
//                    fileTags.setTagId(tagId);
//                    fileTagsFeign.add(fileTags);
//                }
//            }

            final boolean b = FileUtil.deleteFile(FILE_PATH + contentFileName);
            System.out.println("删除内容本地>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + b);
        }

        List<String> tags = new ArrayList<>();
        Element tagDiv = document.getElementsByClass("tagsL z").get(0);
        Elements tagSource = tagDiv.getElementsByTag("a");
        for (Element element : tagSource) {
            System.out.println(element.text());
            tags.add(element.text());
        }

        if (!tags.isEmpty()) {
            for (String tag : tags) {
                // 保存标签
                Tags t = new Tags();
                t.setValue(tag);
                t.setConfidence(99.0);
                tagsService.save(t);

                // 保存关系
                ContentTags ct = new ContentTags();
                ct.setContentId(contentId);
                ct.setTagId(t.getId());
                contentTagsService.save(ct);
            }
        }

        System.out.println("当前标签个数>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + tags.size());
    }

    /**
     * 保存用户信息
     *
     * @param url
     */
    public Map<String, Object> getUserInfo(String url) {
        Map<String, Object> map = new HashMap<>();
        Document document = JsoupUtil.getDocument(url);

        // 获取用户信息
        String userAvatarUrl = document.getElementsByClass("artUser z").get(0).getElementsByTag("img").get(0).attr("src");
        String userName = document.getElementsByClass("homeName z").get(0).getElementsByTag("a").get(0).text();
        Elements userGender = document.getElementsByClass("homeName z").get(0).getElementsByClass("girl");

        System.out.println("抓取用户姓名>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + userName);

        /**
         * 1.下载用户头像
         * 2.创建用户对象
         */
        long userId = 0;
        User param = new User();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nick_name", userName);
//        User userData = userService.list(queryWrapper);
        List<User> list = userService.list(queryWrapper);

        if (ObjectUtils.isEmpty(list)) {
            String availableUrl = getAvailableUrl(userAvatarUrl);

            // 头像不合法就跳过
            if (StringUtils.isEmpty(availableUrl)) {
                return new HashMap<>();
            }

            // 如果是有效地址才扒取
            if (StringUtils.isNotEmpty(availableUrl)) {
                String fileName = FileUtil.downloadPicture(availableUrl, FILE_PATH);
                User user = new User();
                user.setAvatarUrl(fileName);
                user.setNickName(userName);
                user.setGender(userGender.isEmpty() ? 0 : 1);
                userService.save(user);
                userId = user.getId();

                // 上传oss -> 头像目录
                String rq = aliYunOssUtil.upload(fileName, "face");
                System.out.println("头像上传阿里>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + rq);

                boolean b = FileUtil.deleteFile(FILE_PATH + fileName);
                System.out.println("删除本地头像>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + b);
            }
        } else {
            userId = list.get(0).getId();
        }

        map.put("userId", userId);
        map.put("document", document);
        return map;
    }

    /**
     * 获取元素集
     */
    public List<String> getContent(String url) {
        Document document = JsoupUtil.getDocument(url);
        List<Element> elements = new ArrayList<>();
        // 头像、美图
        Elements e1 = document.getElementsByClass("txList ");
        Elements e2 = document.getElementsByClass("txList listRight");
        // 壁纸
        Elements e3 = document.getElementsByClass("txList-sj ");
        Elements e4 = document.getElementsByClass("txList-sj listRight");
        if (!e1.isEmpty()) {
            elements.addAll(e1);
        }
        if (!e2.isEmpty()) {
            elements.addAll(e2);
        }
        if (!e3.isEmpty()) {
            elements.addAll(e3);
        }
        if (!e4.isEmpty()) {
            elements.addAll(e4);
        }
        System.out.println("抓取内容个数>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + elements.size());

        // 拼接元素地址
        List<String> list = elements.stream().map(element -> "https://www.woyaogexing.com" + element
                .getElementsByTag("a")
                .get(0).attr("href")).collect(Collectors.toList());
        return list;
    }

    /**
     * 获取源地址
     *
     * @param url
     * @return
     */
    public String getAvailableUrl(String url) {

        if (url.toUpperCase().endsWith("JPEG")
                || url.toUpperCase().endsWith("JPG")
                || url.toUpperCase().endsWith("PNG")
                || url.toUpperCase().endsWith("BMP")) {
            StringBuffer sb = new StringBuffer();

            char[] chars = url.toCharArray();
            // 获取后缀
            List<Integer> index = new ArrayList<>();
            for (int i = 0; i < chars.length; i++) {
                if (String.valueOf(chars[i]).equals(".")) {
                    index.add(i);
                }
            }

            if (url.indexOf("!") != -1) {
                Optional<Integer> max = index.stream().max((a, b) -> a - b);
                String availableUrl = url.substring(0, url.indexOf("!")) + url.substring(max.get());
                sb.append(availableUrl);
            } else {
                sb.append(url);
            }


            if (!sb.toString().contains("http")) {
                sb.insert(0, "http:");
                return sb.toString();
            }

        }

        return null;
    }
}
